package com.check.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.check.dao.AgencyDao;
import com.check.dao.PersonDao;
import com.check.dao.PersonRoleDao;
import com.check.dao.PersonTempDao;
import com.check.dao.WorkRoleDao;
import com.check.pojo.Opr;
import com.check.pojo.Org;
import com.check.pojo.PageObject;
import com.check.pojo.WorkRole;
import com.check.service.AgencyService;
import com.check.service.PersonService;
import com.check.service.WorkRoleService;
import com.check.util.StringUtil;

@Service("personService")
public class PersonServiceImpl  implements PersonService{

	@Autowired
	@Qualifier("agencyDao")
	private AgencyDao agencyDao;
	
	@Autowired
	@Qualifier("personDao")
	private PersonDao personDao;
	
	@Autowired
	@Qualifier("personRoleDao")
	private PersonRoleDao personRoleDao;
	
	@Autowired
	@Qualifier("personTempDao")
	private PersonTempDao personTempDao;
	
	public List<Opr> findAllOpr(PageObject pageObject,String orgId) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("startNum", pageObject.getStartNum());
		map.put("endNum", pageObject.getEndNum());
		map.put("orgId", orgId);
		return personDao.findAllOpr(map);
	}
	
	public List<Opr> findAllOprByUpId(String upId){
		
		return personDao.findAllOprByUpid(upId);
	}
	
	public String getPersonTreeJson(String upid,String flag){
		Map map=new HashMap();
		map.put("upid", upid);
		map.put("flag", flag);
		List<Org> orgList = agencyDao.findAllOrgByUpId(map);
		List<Opr> oprList = personDao.findAllOprByUpid(upid);
		StringBuilder sb=new StringBuilder("[");
		
		for (int i = 0; i < oprList.size(); i++) {
			Opr opr = oprList.get(i);
			
			if(StringUtil.isBlank(opr.getDeptId()) && StringUtil.isBlank(opr.getRoomId()) && StringUtil.isBlank(opr.getOrgId())){
				continue;
			}
			
			sb.append("{");
			sb.append("id:'"+opr.getOprId()+"',");
			sb.append("pId:'"+opr.getOrgId()+"',");
			sb.append("name:'"+opr.getOprName()+"',");
			sb.append("open:false,");
			sb.append("type:'1'");
			if(i==oprList.size()-1){
				sb.append("}");
			}else{
				sb.append("},");
			}
	}
		if(orgList.size()>0 && oprList.size()>0){
			sb.append(",");
		}
		
		for (int i = 0; i < orgList.size(); i++) {
				Org org=orgList.get(i);
				if(!"2".equals(flag)){
					if(upid.equals(org.getO_id())){
						continue;
					}
				}
				sb.append("{");
				sb.append("id:'"+org.getO_id()+"',");
				sb.append("pId:'"+org.getO_upid()+"',");
				sb.append("name:'"+org.getO_name()+"',");
				if(org.getO_orgcount()>0){
					sb.append("isParent:true,");
				}else if(org.getO_orgcount()==0 && org.getO_oprcount()>0){
					sb.append("isParent:true,");
				}else{
					sb.append("isParent:false,");
				}
				sb.append("type:'0',");
				sb.append("open:false");
				if(i==orgList.size()-1){
					sb.append("}");
				}else{
					sb.append("},");
				}
		}
	
	
		sb.append("]");
		
		return sb.toString();
		
	}

	public Opr findByOprId(String oprId){
		Opr opr=null;
		try {
			opr=personDao.findOprById(oprId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return opr;
	}
	
	public int findOprCountOfOrg(String orgId){
		return personDao.findOprCountOfOrg(orgId);
	}
	
	public boolean deleteOpr(String oprId){
		boolean flag=false;
		try {
			personDao.deleteOpr(oprId);
			personTempDao.deleteOprTemp(oprId);
			personRoleDao.deletePersonRole(oprId);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	public boolean updateOpr(Opr opr){
		boolean flag=false;
		try {
			personDao.updateOpr(opr);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}
	
	public boolean updateOprPassword(Opr opr){
		boolean flag=false;
		try {
			personDao.updateOprPassword(opr);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	public boolean updateMarkResets(String oprId){
		boolean flag=false;
		try {
			personDao.updateMarkResets(oprId);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}
	
	public boolean insertOpr(Opr opr){
		boolean flag=false;
		try {
			personDao.insertOpr(opr);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	public boolean updatePasswd(Opr opr) {
		boolean flag=false;
		try {
			personDao.updatePasswd(opr);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}
	
	public List<Opr> findAllMarkOprByOrgId(String orgId){
		return personDao.findAllMarkOprByOrgId(orgId);
	}
	
	public List<Opr> findAllManager(PageObject pageObject){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("startNum", pageObject.getStartNum());
		map.put("endNum", pageObject.getEndNum());
		return personDao.findAllManager(map);
	}

	public int findAllManagerCount() {
		return personDao.findAllManagerCount();
	}

	public String getPersonTreeJsonByOprId(String oprId, String orgId,String flag) {
		Org findOrg=new Org();
		findOrg.setO_upid(orgId);
		findOrg.setOprId(oprId);
		List<Org> orgList = agencyDao.findAllOrgTreeByOprId(findOrg);
		
		List<Opr> oprList = new ArrayList<Opr>();
		if(!"3".equals(flag)){
			oprList=personDao.findAllOprByUpid(orgId);
		}
				
		StringBuilder sb=new StringBuilder("[");
		
		for (int i = 0; i < oprList.size(); i++) {
			Opr opr = oprList.get(i);
			
			if(StringUtil.isBlank(opr.getDeptId()) && StringUtil.isBlank(opr.getRoomId()) && StringUtil.isBlank(opr.getOrgId())){
				continue;
			}
			sb.append("{");
			sb.append("id:'"+opr.getOprId()+"',");
			sb.append("pId:'"+opr.getOrgId()+"',");
			sb.append("name:'"+opr.getOprName()+"',");
			sb.append("open:false,");
			sb.append("type:'1'");
			if(i==oprList.size()-1){
				sb.append("}");
			}else{
				sb.append("},");
			}
	}
		if(orgList.size()>0 && oprList.size()>0){
			sb.append(",");
		}
		
		for (int i = 0; i < orgList.size(); i++) {
				Org org=orgList.get(i);
				sb.append("{");
				sb.append("id:'"+org.getO_id()+"',");
				sb.append("pId:'"+org.getO_upid()+"',");
				sb.append("name:'"+org.getO_name()+"',");
				if(org.getO_orgcount()>0){
					sb.append("isParent:true,");
				}else if(org.getO_orgcount()==0 && org.getO_oprcount()>0){
					sb.append("isParent:true,");
				}else{
					sb.append("isParent:false,");
				}
				sb.append("type:'0',");
				sb.append("nocheck:true,");
				sb.append("open:false");
				if(i==orgList.size()-1){
					sb.append("}");
				}else{
					sb.append("},");
				}
		}
	
	
		sb.append("]");
		
		return sb.toString();
	}
	
	public boolean updateFileId(Opr opr){
		boolean flag=false;
		try {
			this.personDao.updateFileId(opr);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
		
	}

	public boolean updateRiseOpr(Opr opr) {
		boolean flag=false;
		try {
			personDao.updatePersonRisk(opr);
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}

	public Opr findRiseById(String oprId) {
		Opr opr=null;
		try {
			opr=personDao.findRiseById(oprId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return opr;
	}

	public boolean updateInitPasswdByWorkRoleId(Opr opr) {
		boolean flag=false;
		try {
			String[] workroles = opr.getWorkroles();
			for (String workRoleId : workroles) {
				Opr newOpr = new Opr();
				newOpr.setWorkRoleId(workRoleId);
				newOpr.setOprPasswd(opr.getOprPasswd());
				personDao.updateInitPasswdByWorkRoleId(opr);
			}
			
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			flag=false;
		}
		return flag;
	}
}
